package com.smart.client.core.notice;

import com.alibaba.fastjson.JSON;
import com.smart.client.core.rule.MemsRuleHandler;
import com.smart.client.core.rule.RainWaterRuleHandler;
import com.smart.client.core.rule.RaySightRuleHandler;
import com.smart.client.core.rule.RuleHandler;
import com.smart.client.mems.MemsResult;
import com.smart.client.rainwater.RainWaterResult;
import com.smart.client.raysight.RaysightResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.smart.client.core.AlarmRuleType.MEMS_SIGN;
import static com.smart.client.core.AlarmRuleType.RAIN_WATER_SIGN;
import static com.smart.client.core.AlarmRuleType.RAY_SIGHT_SIGN;
import static com.smart.client.core.rule.AbstractRuleHandler.RULE_HANDLER_SUFFIX;
import static com.smart.client.core.rule.DefaultRuleHandler.TOPIC_NAME;
import static com.smart.client.core.rule.MemsRuleHandler.MEMS_TOPIC;
import static com.smart.client.core.rule.RainWaterRuleHandler.RAIN_WATER_TOPIC;

/**
 * Created By Li Jiaxi
 * <br/>Date: 2023/4/12 15:40
 * <br/>Description:
 */
@Slf4j
@Component
public class DefaultEventNotice extends AbstractEventNotice {


    protected DefaultEventNotice(Map<String, RuleHandler<?>> ruleHandler) {
        super(ruleHandler);
    }

    @Override
    public void triggerNotice(Object message) {
        String msg = message.toString();
        log.debug("====>>>> 触发通知,接收消息：[{}],调用告警类型规则拦截器[AlarmTypeRuleHandler]", msg);
        // 若推送数据中不存在topic字段，则为 雷视数据
        if (!msg.contains(TOPIC_NAME)) {
            // 雷视 规则处理
            List<RaysightResult> raysightResultList = JSON.parseArray(msg, RaysightResult.class);
            if (!CollectionUtils.isEmpty(raysightResultList)) {
                RaySightRuleHandler ruleHandler = (RaySightRuleHandler) getRuleHandler(RAY_SIGHT_SIGN + RULE_HANDLER_SUFFIX);
                ruleHandler.handler(raysightResultList);
                log.debug("====>>>> 针对雷视数据告警规则解析完成.....");
                return;
            }
            log.error("====>>>> 当前推送数据[{}] 系统还未对接,请联系系统管理员.", msg);
        }
        // 若推送数据中存在topic字段，则为 MEMS/水位、雨量数据
        if (msg.contains(RAIN_WATER_TOPIC)) {
            log.debug("这是水位、雨量");
        }
        if (msg.contains(MEMS_TOPIC)) {
            log.debug("这是MEMS");
        }
        // 雨量、水位 规则处理
        RainWaterResult rainWaterResult = JSON.parseObject(msg, RainWaterResult.class);
        if (Objects.nonNull(rainWaterResult) && Objects.equals(RAIN_WATER_TOPIC, rainWaterResult.getTopic())) {
            RainWaterRuleHandler ruleHandler = (RainWaterRuleHandler) getRuleHandler(RAIN_WATER_SIGN + RULE_HANDLER_SUFFIX);
            ruleHandler.handler(rainWaterResult);
            log.debug("====>>>> 针对水位、雨量数据告警规则解析完成.....");
            return;
        }
        // mems 规则处理
        MemsResult memsResult = JSON.parseObject(msg, MemsResult.class);
        if (Objects.nonNull(memsResult) && Objects.equals(MEMS_TOPIC, memsResult.getTopic())) {
            MemsRuleHandler ruleHandler = (MemsRuleHandler) getRuleHandler(MEMS_SIGN + RULE_HANDLER_SUFFIX);
            ruleHandler.handler(memsResult);
            log.debug("====>>>> 针对MEMS数据告警规则解析完成.....");
            return;
        }

        log.error("====>>>> 当前推送数据[{}] 系统还未对接,请联系系统管理员.", msg);
    }

    /**
     * 获取规则拦截器
     *
     * @param name bean名称
     * @return 规则拦截器
     */
    protected RuleHandler<?> getRuleHandler(String name) {
        if (!ruleHandler.containsKey(name)) {
            throw new RuntimeException("该数据规则还未实现.");
        }
        return ruleHandler.get(name);
    }
}
